import axios from 'axios';
import { UserInfo as CurrentUserInfo } from '@/store/modules/user/types';

export interface LoginData {
  phone: string;
  pwd: string;
}

export interface UserInfo {
  accountNonExpired: string;
  accountNonLocked: boolean;
  authCode2RestPattern: string;
  authCodes: string;
  authorities: string;
  credentialsNonExpired: string;
  currentOrgId: string;
  currentOrgName: string;
  enabled: string;
  id: string;
  isVisble: string;
  loginName: string;
  loginPwd: string;
  loginPwdTxt: string;
  name: string;
  password: string;
  permissions: string;
  positionIds: string;
  roleIds: string;
  sessionId: string;
  tenantId: string;
  ternminal: string;
  username: string;
  verificationCode: string;
}

export interface LoginRes {
  authorization: string;
  sessionId: string;
}

export interface UserVo {
  groupId: Array<string>;
  id: string;
  loginName: string;
  loginPwdTxt: string;
  organizationId: string;
  personId: string;
  positions: Array<{
    organizationId: string;
    positionId: string;
    userPositionType: string;
  }>;
  roleId: Array<string>;
  sortNumber: string;
  sysInnerFlag: string;
  userPositionType: string;
  visibleFlag: string;
}

export interface UserEditVo {
  id: string;
  loginName: string;
  personId: string;
  positionId: Array<string>;
  roleId: Array<string>;
}

export interface UserDetailVo {
  address: string;
  cardId: string;
  email: string;
  firstEdu: string;
  gender: string;
  highEdu: string;
  id: string;
  loginName: string;
  name: string;
  namePinyin: string;
  officePhone: string;
  personId: string;
  phone: string;
  positionId: string;
  postCode: string;
  roleId: string;
  sortNumber: 0;
  type: string;
}

export interface UserListVo {
  id: string;
  age: string;
  gender: string;
  loginName: string;
  name: string;
  phone: string;
  positionInfo: [
    {
      authControl: true;
      code: string;
      createOrgId: string;
      createOrgName: string;
      createTime: string;
      creator: string;
      creatorName: string;
      deleteFlag: string;
      gradeId: number;
      gradeName: string;
      hasAuthCreateOrgCollection: [string];
      hasAuthCreatorCollection: [string];
      id: string;
      leaderPositionId: string;
      name: string;
      newVersionTime: number;
      operator: string;
      operatorName: string;
      simpleName: string;
      sysInnerFlag: string;
      tenantId: string;
      type: string;
      versionTime: number;
    }
  ];
  roleInfo: [
    {
      authControl: true;
      createOrgId: string;
      createOrgName: string;
      createTime: string;
      creator: string;
      creatorName: string;
      deleteFlag: string;
      description: string;
      hasAuthCreateOrgCollection: [string];
      hasAuthCreatorCollection: [string];
      id: string;
      name: string;
      newVersionTime: number;
      operator: string;
      operatorName: string;
      parentId: string;
      sysAdminFlag: string;
      sysInnerFlag: string;
      tenantId: string;
      type: string;
      versionTime: number;
    }
  ];
  userId: string;
}

// extends Partial<PersonVo>
export interface UserListParams {
  gender: string;
  loginName: string;
  page: number;
  personId: string;
  phone: string;
  positionId: string;
  roleId: string;
  rows: number;
  tenantId: string;
  userId: string;
  organizationId: string;
}

export interface UserListRes {
  rows: UserListVo[];
  total: number;
}
export interface TUserGroup {
  id: string;
  name: string;
}
export interface TOrganization {
  id: string;
  name: string;
}
export interface TPosition {
  id: string;
  name: string;
}
export interface TRole {
  id: string;
  name: string;
}
export interface PortalQueryVO {
  id: string;
  name: string;
}
// 详情出参
export interface UserQueryVO {
  groups: TUserGroup[];
  loginName: string; // 用户名
  organizations: TOrganization[];
  positions: TPosition[];
  roles: TRole[];
  sortNumber: number;
  portalQueryVOS: PortalQueryVO[];
  visibleFlag: string; // 可视标识
}
// export interface UserAuthVO {
//   address: string;
//   authCodes: [string];
//   card: string;
//   code: string;
//   createOrgId: string;
//   createOrgName: string;
//   createTime: string;
//   createTime4View: string;
//   creator: string;
//   creatorName: string;
//   currentOrgId: string;
//   currentOrgName: string;
//   deleteFlag: string;
//   email: string;
//   firstEdu: string;
//   forbidVisitPaths: [];
//   gender: string;
//   highEdu: string;
//   id: string;
//   isVisible: string;
//   loginName: string;
//   loginPwdTxt: string;
//   name: string;
//   namePinYin: string;
//   newVersionTime: 0;
//   operator: string;
//   operatorName: string;
//   phone: string;
//   positions: [];
//   postCode: string;
//   resources: [];
//   roles: [];
//   sessionId: string;
//   status: string;
//   statusName: string;
//   tenantId: string;
//   terminal: string;
//   type: string;
//   versionTime: 0;
// }

export function login(data: LoginData) {
  return axios.post<LoginRes>('voucher/user/login', data);
}

export function getCurrentUserInfo() {
  return axios.post<CurrentUserInfo>('tenant/getCurrentUserInfo');
}

export function logout() {
  // return axios.post<LoginRes>('/api/user/logout');
}

export function getUserInfo() {
  return axios.post<UserInfo>('tenant/getUserInfo');
}

export function selectPageUser(params: UserListParams) {
  return axios.post<UserListRes>('tenant/user/selectPageUser', params);
}

export function selectUserById(id: string) {
  return axios.post<UserDetailVo>(`tenant/user/detailById/${id}`);
}

export function createUser(params: UserVo) {
  return axios.post<UserVo>('tenant/user/createUser', params);
}

export function updateUser(params: UserEditVo) {
  return axios.post<UserEditVo>('tenant/user/updateUser', params);
}

export function delUser(id: string) {
  return axios.get<string>(`tenant/user/deleteUser/${id}`);
}
export function detailById(id: string) {
  return axios.get<UserQueryVO>(`tenant/user/detailById/${id}`);
}
export interface UserPageQueryVO {
  id: string;
}
// 根据菜单id查询绑定的用户
export function selectUserByBindMenu(menuId: string) {
  return axios.post<UserPageQueryVO[]>(
    `tenant/user/selectUserByBindMenu?menuId=${menuId}`
  );
}
// 绑定用户菜单
export function bindUserAndMenu(param: {
  businessId: string;
  userIds: Array<string>;
}) {
  return axios.post('tenant/user/bindUserAndMenu', param);
}
// 根据组织id查询绑定的用户
export function selectUserByBindOrganization(orgId: string) {
  return axios.post<UserPageQueryVO[]>(
    `tenant/user/selectUserByBindOrganization?orgId=${orgId}`
  );
}
// 根据用户id查询绑定的其他用户
export function selectOtherUserByBindUser(userId: string) {
  return axios.post<UserPageQueryVO[]>(
    `tenant/user/selectOtherUserByBindUser?userId=${userId}`
  );
}
// 组织下绑定多个用户
export function bindUserAndDataAuthOrganization(param: {
  businessId: string;
  userIds: Array<string>;
}) {
  return axios.post('tenant/user/bindUserAndDataAuthOrganization', param);
}
// 资源权限绑定用户
export function bindUserFunctionalAuthority(param: {
  businessId: string;
  userIds: Array<string>;
}) {
  return axios.post('tenant/user/bindUserAndResource', param);
}
// 用户下绑定多个其他用户
export function bindUserAndOtherUser(param: {
  businessId: string;
  userIds: Array<string>;
}) {
  return axios.post('tenant/user/bindUserAndOtherUser', param);
}

// 机器人
export function getUserByGroupId(groupId: string, keyWords: string) {
  return axios.get<any>(
    `tenant/user/getUserByGroupId/${groupId}?keyWords=${keyWords}`
  );
}
